package net.hn.hnms.biz.emgcy.template.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 自定义时间格式转换器
 * <p>
 * 格式：yyyy-MM-dd HH:mm:ss 可任意修改
 *
 * @author admin
 */
public class LocalDateTimeConverter implements Converter<LocalDateTime> {
    private static final  DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                           GlobalConfiguration globalConfiguration) {
        String value = cellData.getStringValue();
        if(StringUtils.isEmpty(value)) {
            return null;
        }
        return LocalDateTime.parse(cellData.getStringValue(), FORMATTER);

    }

    @Override
    public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return new WriteCellData<>(FORMATTER.format(value));
    }


}

